package com.runa.monitor.platinterface.backwork.impl;

import com.alibaba.fastjson.JSON;
import com.runa.monitor.comm.dto.newComm.AsynResult;
import com.runa.monitor.comm.dto.newComm.PanelVO;
import com.runa.monitor.comm.dto.newComm.ValveVO;
import com.runa.monitor.comm.entity.CommPanel;
import com.runa.monitor.comm.entity.CommValve;
import com.runa.monitor.platinterface.backwork.function.TaskFunction;
import com.runa.persistence.util.SpringContextUtil;
import com.runa.monitor.platinterface.mqtt.MqttUtil;
import com.runa.monitor.platinterface.netservice.connect.commstatus.CommStatus;
import com.runa.monitor.platinterface.netservice.equipment.AutoUpwardDriver;
import com.runa.monitor.platinterface.netservice.equipment.NetEqu;
import com.runa.monitor.platinterface.platService.RepositoryService;
import com.runa.rccprotocol.dto.AffirmDeny;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

@Slf4j
public class ReportTaskFunction implements TaskFunction<NetEqu<String>, Object, Void> {

    private NetEqu netEqu;
    private final RepositoryService repositoryService;
    private final String netId;
    @Override
    public void setStatus(NetEqu netEqu) {
        this.netEqu = netEqu;
    }

    public ReportTaskFunction(String netId) {
        this.repositoryService = SpringContextUtil.getBean(RepositoryService.class);
        this.netId = netId;
    }

    @Override
    public void doApply(CommStatus status, Object result) {
        log.debug("主动上报数据任务触发---" + JSON.toJSONString(result) + "---" + status);
        if (result instanceof List) {
            List list = (List) result;
            AsynResult asynResult = new AsynResult();
            for (Object o : list) {
                if (netEqu instanceof AutoUpwardDriver) {
                    AutoUpwardDriver driver = (AutoUpwardDriver) netEqu;
                    driver.upwardCallback(list);//每次上报触发回调
                }
                if (o instanceof ValveVO) {
                    ValveVO n = (ValveVO) o;
                    if (n.getValveSerial() != null) {
                        CommValve commValve;
                        if (netId == null) {
                            commValve = repositoryService.getCommValveByNodeSerial(n.getValveSerial());
                        } else {
                            commValve = repositoryService.getCommValveByNodeSerialForNetEqu(n.getValveSerial(), netId);
                        }
                        if (null == commValve) {
                            log.error("{} {} 主动上报，设备不存在", netId, n.getValveSerial());
                            return;
                        }
                        asynResult.setIdentity(commValve.getId());
                    } else if (n.getIndex() != null && netId != null) {
                        CommValve commValve = repositoryService.getCommValveByNodeIndexForNetEqu(n.getIndex(), netId);
                        if (null == commValve) {
                            log.error("{} {} 主动上报，设备不存在", netId, n.getIndex());
                            return;
                        }
                        asynResult.setIdentity(commValve.getId());
                    } else {
                        log.error("主动上报数据任务 该设备未能成功转换GUID：asynResult" + JSON.toJSONString(asynResult));
                        return;
                    }
                    asynResult.setContent(o);
                    MqttUtil.send(ValveVO.TOPIC, asynResult);
                } else if (o instanceof List) { //温度面板
                    List<PanelVO> panelVOS = (List<PanelVO>) o;
                    for (PanelVO panelVO : panelVOS) {
                        if (panelVO.getSerial() != null) {
                            CommPanel commPanel;
                            if (netId == null) {
                                commPanel = repositoryService.getCommPanelByNodeSerial(panelVO.getSerial());
                            } else {
                                commPanel = repositoryService.getCommPanelByNodeSerialForNetEqu(panelVO.getSerial(), netId);
                            }
                            if (commPanel == null) {
                                log.error("{} {} 主动上报，设备不存在", netId, panelVO.getSerial());
                                return;
                            }
                            asynResult.setContent(o);
                            asynResult.setIdentity(commPanel.getId());
                            MqttUtil.send(PanelVO.TOPIC, asynResult);
                        } else {
                            log.error("主动上报数据任务 该设备未能成功转换GUID：asynResult" + JSON.toJSONString(asynResult));
                        }
                    }
                }
            }
        } else if (result instanceof AffirmDeny) {
            AffirmDeny affirmDeny = (AffirmDeny) result;
            log.info("主动上报设备:{} 控制结果:{}", affirmDeny.getAddress(), affirmDeny.getData());
        }
    }

    @Override
    public Void waitResult() {
        return null;
    }

    @Override
    public String getSerial() {
        return null;
    }

    @Override
    public int getPriority() {
        return 0;
    }
}
